package com.party.core.service.photo.biz;

import com.party.common.utils.StringUtils;
import com.party.core.model.photo.*;
import com.party.core.service.photo.*;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description: 相册数量统计业务
 * @Author: yangshoukun
 * @Date: 2018/6/4 10:37
 */
@Service
public class PhotosCountBizService {

    @Autowired
    private IPhotosTagService photosTagService;

    @Autowired
    private IPhotosImgInfoService photosImgInfoService;

    @Autowired
    private IPhotoProjectService photoProjectService;

    @Autowired
    private IPhotosMemberOperateService photosMemberOperateService;

    @Autowired
    private IPhotosMemberOperateDetailService photosMemberOperateDetailService;

    /**
     * 统计图片数量
     * @param photosId
     * @param tagIds
     */
    public void CountImgNum(String photosId, String[] tagIds) {
        // 添加照片添加各照片数量冗余数
        int size = 100;
        int allCount = photosImgInfoService.getCount(photosId, null);
        PhotoProject photoProject = photoProjectService.get(photosId);
        photoProject.setImgNum(allCount);
        photoProjectService.update(photoProject);

        PhotosTag param = new PhotosTag();
        param.setPhotosId(photosId);

        // 时间线
        param.setName(PhotosTag.ALBUM_TIME_LINE);
        PhotosTag dbTag = photosTagService.findByName(param);
        dbTag.setImgNum(allCount);
        photosTagService.update(dbTag);

        // 热门
        param.setName(PhotosTag.ALBUM_HOT);
        dbTag = photosTagService.findByName(param);
        dbTag.setImgNum(allCount > size ? size : allCount);
        photosTagService.update(dbTag);

        if (null != tagIds) {
            for (String tagId : tagIds) {
                if (StringUtils.isNotEmpty(tagId) && !"0".equals(tagId)) {
                    PhotosTag photosTag = photosTagService.get(tagId);
                    // 时间线和热门不用管
                    if (PhotosTag.ALBUM_HOT.equals(photosTag.getName()) || PhotosTag.ALBUM_TIME_LINE.equals(photosTag.getName())) {

                    } else {
                        int tagNum = photosImgInfoService.getCount(photosId, tagId);
                        photosTag.setImgNum(tagNum);
                        photosTagService.update(photosTag);
                    }
                }
            }
        }
    }

    /**
     * 相册用户操作统计表
     * @param photosId
     * @param memberId
     */
    public String countOperate(String photosId, String memberId) {
        if (StringUtils.isEmpty(photosId) || StringUtils.isEmpty(memberId)) {
            return null;
        }

        PhotosMemberOperate param = new PhotosMemberOperate();
        param.setPhotosId(photosId);
        param.setMemberId(memberId);
        List<PhotosMemberOperate> list = photosMemberOperateService.list(param);
        if (CollectionUtils.isEmpty(list)) {
            param.setCreateBy(memberId);
            param.setUpdateBy(memberId);
            param.setOperateNum(1);
            return photosMemberOperateService.insert(param);
        } else {
            PhotosMemberOperate photosMemberOperate = list.get(0);
            photosMemberOperate.setUpdateDate(new Date());
            photosMemberOperate.setOperateNum(photosMemberOperate.getOperateNum() + 1);
            photosMemberOperateService.update(photosMemberOperate);
            return photosMemberOperate.getId();
        }
    }

    /**
     * 相册用户操作详情统计
     * @param photosId: 相册id
     * @param memberId: 用户id
     * @param operateId: 用户操作统计id
     * @param imgId: 图片id
     * @param operateEnum: 操作枚举
     */
    public void countOperateDetail(String photosId, String memberId, String operateId, String imgId, OperateEnum operateEnum) {
        if (StringUtils.isEmpty(photosId) || StringUtils.isEmpty(memberId) || StringUtils.isEmpty(operateId) ||
                StringUtils.isEmpty(imgId) || null == operateEnum) {
            return;
        }

        PhotosMemberOperateDetail photosMemberOperateDetail = new PhotosMemberOperateDetail();
        photosMemberOperateDetail.setImgId(imgId);
        photosMemberOperateDetail.setCode(operateEnum.getCode());
        photosMemberOperateDetail.setName(operateEnum.getOperate());
        photosMemberOperateDetail.setMemberId(memberId);
        photosMemberOperateDetail.setOperateId(operateId);
        photosMemberOperateDetail.setPhotosId(photosId);
        photosMemberOperateDetail.setCreateBy(memberId);
        photosMemberOperateDetail.setUpdateBy(memberId);
        photosMemberOperateDetailService.insert(photosMemberOperateDetail);

        PhotosImgInfo photosImgInfo = photosImgInfoService.get(imgId);
        photosImgInfo.setOperateId(operateId);
        photosImgInfo.setLastMember(photosMemberOperateDetail.getMemberId());
        photosImgInfo.setLastTime(photosMemberOperateDetail.getCreateDate());
        photosImgInfoService.update(photosImgInfo);
    }

    /**
     * 统计图片的操作次数
     * @param imgId
     */
    public void countImgOperate(String imgId) {
        if (StringUtils.isEmpty(imgId)) {
            return;
        }
        PhotosImgInfo imgInfo = photosImgInfoService.get(imgId);
        if (null == imgInfo) {
            return;
        }
        imgInfo.setOperateNum((imgInfo.getOperateNum() == null ? 0 : imgInfo.getOperateNum()) + 1);
        photosImgInfoService.update(imgInfo);
    }
}
